<?php
class News_NewsController extends Promotor_Controller_Action_Scaffold {
	
	public $skin = array(
		// forsuje domyslny template i style na admin
		'template' => 'admin',
		'style' => 'default',
		// layout
		'layout' => 'admin_news',
		// rozszerzam konfiguracje
		'config' => array(
			'filename' => 'backend_config.ini'
		),

		// specjalizuje template dla akcji
		'index' => array(
			'layout' => 'news_index'
		),
		'display' => array(
			'layout' => 'news_display'
		)
	);

//	public $ajaxable = array(
//		'list' => array('dojo','ext')
//	);
	
	public $acl = array(
		'list' => 'list',
		'add' => 'create',
		'addfromodt' => 'create',
		'edit' => 'update',
		'display' => 'show',
		'delete' => 'delete'
	);

	/**
	 * @see Promotor_Controller_Action_Scaffold specialization
	 */
	protected $_modelClass = 'News_Model_News';
	protected $_formAddClass = 'News_Form_NewsAdd';
	protected $_formEditClass = 'News_Form_NewsEdit';
	protected $_formRemoveClass = 'News_Form_NewsRemove';
	
	protected $_addPostObservableName = 'news_add_post';
	protected $_editPostObservableName = 'news_edit_post';
	
//	public function init() {
//		/* @var Zend_Controller_Action_Helper_AjaxContext */
//		$ajaxContext = $this->_helper->getHelper('AjaxContext');
//		if (!$ajaxContext->hasContext('dojo')) {
//			$ajaxContext->addContext('dojo', array(
//				'suffix' => 'dojo'
//			));
//		}
//		if (!$ajaxContext->hasContext('ext')) {
//			$ajaxContext->addContext('ext', array(
//				'suffix' => 'ext'
//			));
//		}
//        $ajaxContext->initContext();
//	}
//
	public function indexAction() {
		// setup
		$config = $this->_helper->config('config.ini');
		$page = $this->_getParam('page');
		$itemCountPerPage = isset($config->pagination->itemCountPerPage)
			? $config->pagination->itemCountPerPage
			: 10;

		// data
		$model = new News_Model_News();
		list($rowset, $select) = $model->fetchAllPublicatedOrderByTimeCreate($page, $itemCountPerPage);
		$this->view->rowset = $rowset;

		// pagination
		$paginator = Zend_Paginator::factory($select);
		$paginator->setCurrentPageNumber($page);
		$paginator->setItemCountPerPage($itemCountPerPage);
		$this->view->paginator = $paginator;
	}

	/**
	 * @return void
	 */
	public function displayAction() {
		$model = new News_Model_News();
		$table = $model->getDbTable();

		if ($this->_hasParam('id')) {
			$row = $model->findById($this->_getParam('id'));
		} else
		if ($this->_hasParam('alias')){
			$row = $model->findByAlias($this->_getParam('alias'));
		} else {
			$this->_helper->viewRenderer->render('display.404');
			return;
		}
		
		if (strlen(@$row['redirect']) > 0) {
			$this->_helper->redirector
				->setCode(301)
				->goToUrlAndExit($row['redirect']);
		}

		$this->view->row = $row;

		if ($model->getStatus() === News_Model_News::FAILURE) {
			$this->_helper->viewRenderer->render('display.404');
		}
	}
	
//	/**
//	 * @return void
//	 */
//	public function listAction() {
//		$model = new News_Model_News();
//		$table = $model->getDbTable();
//
//		/* @var $rq Zend_Controller_Request_Http */
//		$rq = $this->getRequest();
//		if ($rq->isPost()) {
//			switch ($rq->getPost('action_type')) {
//				case 'update':
//					if (null !== $rq->getPost('editable')) {
//						if ($this->_helper->acl->isAllowed('update')) {
//							$data = $rq->getPost('editable');
//							$model->editableUpdate($data);
//							$this->_helper->flashMessenger($model->getStatus());
//						}
//					}
//					break;
//				case 'delete':
//					if (null !== $rq->getPost('action_checked')) {
//						if ($this->_helper->acl->isAllowed('delete')) {
//							$data = $rq->getPost('action_checked');
//							$model->editableDelete($data);
//							$this->_helper->flashMessenger($model->getStatus());
//						}
//					}
//					break;
//			}			
//		}
//
//		// setup data grid
//		$config = $this->_helper->config('news.xml');
//		$grid = KontorX_DataGrid::factory($model->selectList(), $config->grid);
//		$grid->setValues($rq->getQuery() + $rq->getPost());
//		
//		$this->_setupDataGridPaginator($grid);
//
//		$this->view->grid = $grid;
//	}
//
//	public function addAction() {
//		$model = new $this->_modelClass();
//		$dbTable = $model->getDbTable();
//
//		$form = new $this->_formAddClass();
//
//		/* @var KontorX_Controller_Action_Helper_Scaffold */
//		$scaffold = $this->_helper->getHelper('scaffold');
//		$scaffold->setDbTable($dbTable)
//				 ->setForm($form)
//				 ->setSuppressArrayNotation(true);
//
//		if ($this->_hasParam('id')) {
//			$scaffold->setRowPK($this->_getAllParams());
//		}
//
//		$scaffold->run(KontorX_Controller_Action_Helper_Scaffold::CREATE);
//
//		$status = $scaffold->getStatus();
//		
//		$falshMessages = $this->_helper->getHelper('flashMessenger');
//		
//		if ($status === KontorX_Controller_Action_Helper_Scaffold::SUCCESS) {
//			$this->_helper->flashMessenger($status);
//
//			$primaryKey = $scaffold->getResult();
//
//			$manager = Promotor_Observable_Manager::getInstance();
//			$list = $manager->notify('news_add_post', $primaryKey);
//
//			$falshMessages->addMessage($status);
//			foreach ($list->getMessages() as $observerName => $messages) {
//				$falshMessages->addMessage(sprintf("%s=%s", implode("<br/>", $messages), $observerName));
//			}
//
//			$this->_helper->redirector->goToAndExit('edit',null,null, array('id' =>$primaryKey));
//		} else {
//			$falshMessages->addMessage($status);
//			$this->view->form = $form;
//		}
//	}
//

	public function editAction() {
		$model = new $this->_modelClass();
		$dbTable = $model->getDbTable();

		$form = new $this->_formEditClass(array(
			'primaryKey' => $this->_getParam('id'),
			'treeValues' => $model->getAttachedSitesId($this->_getParam('id')),
			'request' => $this->getRequest()
		));

		$scaffold = $this->_helper->getHelper('scaffold');
		$scaffold
			->setDbTable($dbTable)
			->setForm($form)
			->setRowPk($this->_getAllParams())
			->run(KontorX_Controller_Action_Helper_Scaffold::UPDATE);

		$flashMessenger = $this->_helper->getHelper('FlashMessenger');
			
		$status = $scaffold->getStatus();
		if ($status === KontorX_Controller_Action_Helper_Scaffold::SUCCESS) {
			$flashMessenger->addMessage($status);

			// uduchamianie powiadomień
			if (null !== $this->_editPostObservableName) {
				$manager = Promotor_Observable_Manager::getInstance();
				$list = $manager->notify(
					$this->_editPostObservableName,
					$scaffold->getRowPK());
	
				$flashMessenger->addMessage($status);
				foreach ($list->getMessages() as $observerName => $messages) {
					// TODO ...
					$flashMessenger->addMessage(sprintf("%s=%s", implode("<br/>", $messages), $observerName));
				}
			}

			$this->_helper->redirector->goTo('edit',null,null,$scaffold->getRowPK());			
		} else
		if ($status === KontorX_Controller_Action_Helper_Scaffold::NO_EXSISTS) {
			$flashMessenger->addMessage($status);
			$this->_helper->redirector->goTo('list');
		} else {
			$flashMessenger->addMessage($status);
			$this->view->form = $form;
		}
	}
	/*public function editAction() {
		$model = new $this->_modelClass();
		$dbTable = $model->getDbTable();

		$form = new $this->_formEditClass(array(
			'primaryKey' => $this->_getParam('id'),
			'treeValues' => $model->getAttachedSitesId($this->_getParam('id'))
		));

		$scaffold = $this->_helper->getHelper('scaffold');
		$scaffold
			->setDbTable($dbTable)
			->setForm($form)
			->setRowPk($this->_getAllParams())
			->run(KontorX_Controller_Action_Helper_Scaffold::UPDATE);

		$status = $scaffold->getStatus();
		
		$falshMessages = $this->_helper->getHelper('flashMessenger');
		
		if ($status === KontorX_Controller_Action_Helper_Scaffold::SUCCESS) {
			$falshMessages->addMessage($status);
			
			$primaryKey = $scaffold->getRowPK();

			$manager = Promotor_Observable_Manager::getInstance();
			$list = $manager->notify('news_edit_post', $primaryKey);

			$falshMessages->addMessage($status);
			foreach ($list->getMessages() as $observerName => $messages) {
				$falshMessages->addMessage(sprintf("%s=%s", implode("<br/>", $messages), $observerName));
			}

			$this->_helper->redirector->goTo('edit',null,null,$primaryKey);
		} else
		if ($status === KontorX_Controller_Action_Helper_Scaffold::NO_EXSISTS) {
			$falshMessages->addMessage($status);
			$this->_helper->redirector->goTo('list');
		} else {
			$falshMessages->addMessage($status);
			$this->view->form = $form;
		}
	}*/
}